#include "air001_Core_Board.h"
#include "air001xx_hal.h"

static void APP_SystemClockConfig(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /* 振荡器配置 */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI |
                                     RCC_OSCILLATORTYPE_LSE;        /* 选择振荡器HSE,HSI,LSI,LSE */
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;                          /* 开启HSI */
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;                          /* HSI 1分频 */
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_24MHz; /* 配置HSI时钟16MHz */
  RCC_OscInitStruct.HSEState = RCC_HSE_OFF;                         /* 关闭HSE */
  /*RCC_OscInitStruct.HSEFreq = RCC_HSE_16_32MHz;*/
  RCC_OscInitStruct.LSIState = RCC_LSI_OFF; /* 关闭LSI */
  RCC_OscInitStruct.LSEState = RCC_LSE_OFF; /* 关闭LSE */
  /*RCC_OscInitStruct.LSEDriver = RCC_LSEDRIVE_MEDIUM;*/
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;         /* 开启PLL */
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; /* 选择时钟源为HSI */
  /* 配置振荡器 */
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  /* 时钟源配置 */
  RCC_ClkInitStruct.ClockType =
    RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1; /* 选择配置时钟 HCLK,SYSCLK,PCLK1 */
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;          /* 选择PLL作为系统时钟 */
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;                 /* AHB时钟 1分频 */
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;                  /* APB时钟 1分频 */
  /* 配置时钟源 */
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
}

int main(void)
{
  HAL_Init();
  APP_SystemClockConfig();

  BSP_LED_Init(LED_RED);
  BSP_USART_Config();
  setvbuf(stdout, NULL, _IONBF, 0);
  printf("CoreClock = %d", SystemCoreClock);
  char buf[10] = {0};
  sprintf(buf, "%.2f", 100.3 - 22.31);
  printf("%s\r\n", buf);

  while (1)
  {
    BSP_LED_Toggle(LED_RED);
    HAL_Delay(500);
  }
}
